﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.SchedulingSystem;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using System;
using System.Data;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Scheduling.Controllers
{
    public class LeaveController : BaseController
    {
        // GET: Scheduling/Leave
        public ActionResult Index()
        {
            ViewBag.StartDate = WebUtils.FirstDayOfMonth(DateTime.Now);
            ViewBag.EndDate = WebUtils.LastDayOfMonth(DateTime.Now);
            return View();
        }

        /// <summary>
        /// 获取休假类型
        /// </summary>
        /// <returns></returns>
        public ActionResult GetHolidayTypeList()
        {
            DataTable dt = null;
            SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
            ResultHelperOfDataTable result = service.GetHolidayTypeInfoList(loginUser.CurrDepCode);
            if (result.S)
            {
                dt = result.D;
            }
            if (dt == null)
            {
                dt.Columns.Add("Id");
                dt.Columns.Add("HolidayTypeName");
            }
            DataRow dr = dt.NewRow();
            dr["Id"] = -1;
            dr["HolidayTypeName"] = "请选择";
            dt.Rows.InsertAt(dr, 0);

            return JsonDate(dt);
        }

        /// <summary>
        /// 休假列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");

            SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
            ResultHelperOfSkip serviceResult = service.GetLeaveList(pageIndex, pageSize, DataConvert.StrToDateTime(startDate, DateTime.Now), DataConvert.StrToDateTime(endDate, DateTime.Now), loginUser.CurrDepCode);
            var result = new { Total = serviceResult.D.RowNum, Rows = serviceResult.D.DT };
            return JsonDate(result);
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            Leave leave = new Leave();
            if (id > 0)
            {
                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfLeave result = service.GetLeave(id.ToString());
                if (result.S)
                {
                    leave = result.D;
                }
            }
            ViewBag.Leave = leave;

            return View();
        }

        /// <summary>
        /// 添加/编辑休假
        /// </summary>
        /// <param name="leave"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(Leave leave)
        {
            OperateResult resultInfo = LeaveManager.SaveLeave(leave);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = LeaveManager.DelLeave(id);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取包含时间段内的所有休假列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetLeaveInfoList()
        {
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");
            string departmentCode = WebUtils.GetString("departmentCode");
            OperateResult resultInfo = LeaveManager.GetLeaveInfoList(startDate, endDate, departmentCode);
            return JsonDate(resultInfo);
        }
    }
}